local constant = require "constant"

local table_insert = table.insert
local math_random = math.random
local table_remove = table.remove
         
-------------------------------------------------
local entity_manager ={}

function entity_manager:init_entitys(max_count)
    self.max_count      = max_count
    self:reset_entitys()
end

function entity_manager:is_round_over()
	return false
end

function entity_manager:reset_entitys()
    self.ids            = {}
    self.entitys        = {}
    self.curr_count     = 0
    self.position       = 1             --lua 下标是1开始
    for i = 1,self.max_count do
        table_insert(self.ids,constant.INVALID_ID)
        table_insert(self.entitys,{id = i,type = 0,path = 0,create_time = 0,duration_time = 0,user = {},
                    point_offset = {x = 0,y = 0},index_id = constant.INVALID_ID,position = constant.INVALID_ID,angle = 0,
                    speed = 1.0,rate = 1,threshold = 0,hit_map = {}})
    end
end

function entity_manager:get_count()
    return self.curr_count
end

function entity_manager:expond(new_size)
    local old_size = self.max_count
    if old_size < new_size then
        for i = old_size + 1, new_size do
            table_insert(self.ids,constant.INVALID_ID)
            table_insert(self.entitys,{id = i,type = 0,path = 0,create_time = 0,duration_time = 0,user = {},
                        point_offset = {x = 0,y = 0},index_id = constant.INVALID_ID,position =  constant.INVALID_ID,angle = 0,
                        speed = 1.0,rate = 1,threshold = 0,hit_map = {}})
        end

        self.max_count = old_size
    end
end

function entity_manager:clear_all()
    self:reset_entitys()
end

function entity_manager:get_entity(id)
    if id < 1 or id > self.max_count then
        errlog("entity delete entity error,INVALID_ID",id) 
        return false
    end

    return self.entitys[id]
end

function entity_manager:new_entity()
    local now_pos = self.position
    for index,entity in ipairs(self.entitys) do
        local now_entity = self.entitys[now_pos]
        if now_entity.index_id == constant.INVALID_ID then
            
            self.curr_count = self.curr_count + 1
            self.ids[self.curr_count] = now_pos
            now_entity.index_id = self.curr_count

            now_pos = now_pos + 1
            if now_pos > self.max_count then
                now_pos = 1
            end
            
            self.position = now_pos
            return now_entity
        end

        now_pos = now_pos + 1
        if now_pos > self.max_count then
            now_pos = 1
        end
    end

    return false
end

function entity_manager:delete_entity(id)
    if id < 1 or id > self.max_count then
        errlog("entity delete entity error,INVALID_ID",id)
        return false
    end

    local entiry = self:get_entity(id)
    local tem_index_id = entiry.index_id
    if  tem_index_id == constant.INVALID_ID then
        errlog("entity delete entity error,entity index_id = INVALID_ID",id)
        return false
    end

    entiry.index_id = constant.INVALID_ID
    entiry.user = {}
    entiry.rate = 1
    entiry.threshold = 0
    self.ids[tem_index_id] = self.ids[self.curr_count]
    if tem_index_id ~= self.curr_count then
        tem_entity = self:get_entity(self.ids[tem_index_id])
        assert(tem_entity)
        tem_entity.index_id = tem_index_id
    end

    self.curr_count = self.curr_count - 1
end

function entity_manager:get_index_entity(index)
    --print("get_index_entity--->",tostring(index),tostring(self.max_count))
    if index < 1 or index > self.max_count then
        errlog("get_index_entity error,INVALID_ID",index)
        return false
    end

    return self:get_entity(self.ids[index])
end

local M = {}
function M.new()
    local o = {}
    return setmetatable(o,{__index = entity_manager})
end

return M